<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      inline
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="系统" prop="systemId">
        <el-select
          v-model="queryParams.systemId"
          placeholder="请选择系统名称"
          clearable
          filterable
          style="width: 240px"
          @change="changeQuerySystem"
        >
          <el-option
            v-for="i in querySystemList"
            :key="i.id"
            :label="i.name"
            :value="i.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="设备" prop="deviceId">
        <el-select
          v-model="queryParams.deviceId"
          placeholder="请选择设备名称"
          clearable
          filterable
          style="width: 240px"
          @change="changeQueryDevice"
        >
          <el-option
            v-for="i in queryDeviceList"
            :key="i.id"
            :label="i.name"
            :value="i.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="检查项" prop="deviceItemId">
        <el-select
          v-model="queryParams.deviceItemId"
          placeholder="请选择检查项名称"
          clearable
          filterable
          style="width: 240px"
        >
          <el-option
            v-for="i in queryItemList"
            :key="i.id"
            :label="i.name"
            :value="i.id"
          />
        </el-select>
      </el-form-item>
      <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">
        搜索
      </el-button>
      <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >
          新建
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" />
    </el-row>
    <el-table v-loading="loading" :data="dataList">
      <el-table-column
        label="问题描述"
        align="center"
        prop="problemContent"
        show-overflow-tooltip
      />
      <el-table-column label="系统" align="center" prop="systemName" />
      <el-table-column label="设备" align="center" prop="deviceName" />
      <el-table-column label="检查项" align="center" prop="deviceItemName" />
      <!-- <el-table-column label="照片" align="center" prop="imageUrl" /> -->
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column label="创建人" align="center" prop="createBy" />
      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >
            修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <el-dialog
      :title="editForm.id ? '编辑问题' : '新增问题'"
      :visible.sync="editShow"
      width="500px"
      :close-on-click-modal="false"
      @close="closeForm"
    >
      <el-form ref="editForm" :model="editForm" :rules="rules" label-width="80px">
        <el-form-item label="系统名称" prop="systemId">
          <el-select
            v-model="editForm.systemId"
            placeholder="请选择系统名称"
            clearable
            filterable
            style="width: 100%"
            @change="changeSystem"
          >
            <el-option
              v-for="i in systemList"
              :key="i.id"
              :label="i.name"
              :value="i.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="设备" prop="deviceId">
          <el-select
            v-model="editForm.deviceId"
            placeholder="请选择设备名称"
            clearable
            filterable
            style="width: 100%"
            @change="changeDevice"
          >
            <el-option
              v-for="i in deviceList"
              :key="i.id"
              :label="i.name"
              :value="i.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="检查项" prop="deviceItemId">
          <el-select
            v-model="editForm.deviceItemId"
            placeholder="请选择检查项名称"
            clearable
            filterable
            style="width: 100%"
          >
            <el-option v-for="i in itemList" :key="i.id" :label="i.name" :value="i.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="问题描述" prop="problemContent">
          <el-input
            v-model="editForm.problemContent"
            placeholder="请输入问题描述"
            type="textarea"
            clearable
          />
        </el-form-item>
        <!-- <el-form-item label="照片" prop="imageUrl">
          <ImageUpload
            v-model="editForm.imageUrl"
            :limit="9"
            :isShowTip="false"
            :isState="false"
          />
        </el-form-item> -->
      </el-form>
      <div slot="footer">
        <el-button @click="closeForm">取 消</el-button>
        <el-button type="primary" @click="submitForm" :loading="submiting"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listSystem,
  listDevice,
  listItem,
  listProblem,
  addProblem,
  updateProblem,
  delProblem,
} from "@/api/device/system";
import { getDeviceList } from "@/api/system/ai";

export default {
  name: "message",
  data() {
    return {
      loading: false, // 遮罩层
      submiting: false, // 提交中
      showSearch: true, // 显示搜索条件
      dataList: [], // 消息列表
      total: 0, // 总条数
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        systemId: null,
        deviceId: null,
        deviceItemId: null,
      },
      querySystemList: [], // 查询系统列表
      queryDeviceList: [], // 查询设备列表
      queryItemList: [], // 查询检查项列表
      systemList: [], // 系统列表
      deviceList: [], // 设备列表
      itemList: [], // 检查项列表
      editShow: false, // 表单弹窗
      editForm: {}, // 表单参数
      rules: {
        // 表单校验
        systemId: [{ required: true, message: "系统不能为空", trigger: "blur" }],
        deviceId: [{ required: true, message: "设备不能为空", trigger: "blur" }],
        deviceItemId: [{ required: true, message: "检查项不能为空", trigger: "blur" }],
        problemContent: [
          { required: true, message: "问题描述不能为空", trigger: "blur" },
        ],
        imageUrl: [{ required: true, message: "照片不能为空", trigger: "blur" }],
      },
    };
  },
  created() {
    this.getQuerySystemList();
    this.getList();
  },
  methods: {
    // 获取系统列表
    getSystemList() {
      listSystem({
        pageNum: 1,
        pageSize: 1000,
      }).then((res) => {
        if (res.code == 200) {
          this.systemList = res.rows;
        }
      });
    },
    // 切换系统
    changeSystem() {
      this.deviceList = [];
      this.itemList = [];
      getDeviceList({
        pageNum: 1,
        pageSize: 1000,
        systemId: this.editForm.systemId,
      }).then((res) => {
        this.deviceList = res.rows;
        if (!res.rows.some((i) => i.id == this.editForm.deviceId)) {
          this.editForm.deviceId = "";
          this.editForm.deviceItemId = "";
        }
      });
    },
    // 切换设备
    changeDevice() {
      if (!this.editForm.deviceId) return;
      this.itemList = [];
      listItem(this.editForm.deviceId).then((res) => {
        this.itemList = res.data;
        if (!res.data.some((i) => i.id == this.editForm.deviceItemId)) {
          this.editForm.deviceItemId = "";
        }
      });
    },
    // 查询获取系统列表
    getQuerySystemList() {
      listSystem({
        pageNum: 1,
        pageSize: 1000,
      }).then((res) => {
        if (res.code == 200) {
          this.querySystemList = res.rows;
        }
      });
    },
    // 查询切换系统
    changeQuerySystem() {
      this.queryParams.deviceId = null;
      this.queryParams.deviceItemId = null;
      getDeviceList({
        pageNum: 1,
        pageSize: 1000,
        systemId: this.queryParams.systemId,
      }).then((res) => {
        this.queryDeviceList = res.rows;
      });
    },
    // 查询切换设备
    changeQueryDevice() {
      this.queryParams.deviceItemId = null;
      listItem(this.queryParams.deviceId).then((res) => {
        this.queryItemList = res.data;
      });
    },
    /** 查询常见问题列表 */
    getList() {
      this.loading = true;
      listProblem(this.queryParams)
        .then((res) => {
          this.dataList = res.rows;
          this.total = res.total;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.pageNum = 1;
      this.queryDeviceList = [];
      this.queryItemList = [];
      this.handleQuery();
    },
    // 新增问题
    handleAdd() {
      this.reset();
      this.getSystemList();
      this.editShow = true;
    },
    // 关闭编辑弹窗
    closeForm() {
      this.reset();
      this.deviceList = [];
      this.itemList = [];
      this.editShow = false;
    },
    // 修改问题
    handleUpdate(row) {
      this.editForm = Object.assign({}, row);
      this.getSystemList();
      this.changeSystem();
      this.changeDevice();
      this.editShow = true;
    },
    // 表单重置
    reset() {
      this.editForm = {
        id: null,
        systemId: null,
        deviceId: null,
        deviceItemId: null,
        problemContent: "",
        imageUrl: "",
      };
      this.resetForm("editForm");
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["editForm"].validate((valid) => {
        if (valid) {
          let request = this.editForm.id ? updateProblem : addProblem;
          this.submiting = true;
          request(this.editForm)
            .then((res) => {
              if (res.code == 200) {
                this.$modal.msgSuccess("操作成功");
                this.editShow = false;
                this.handleQuery();
              }
            })
            .finally(() => {
              this.submiting = false;
            });
        }
      });
    },
    // 删除问题
    handleDelete(row) {
      this.$modal
        .confirm("确认删除该条问题描述吗？")
        .then(() => {
          delProblem(row.id).then((res) => {
            if (res.code == 200) {
              this.$modal.msgSuccess("删除成功");
              this.getList();
            }
          });
        })
        .catch(() => {});
    },
  },
};
</script>
<style lang="scss" scoped></style>
